package cn.hairui.blog.service.impl;

import cn.hairui.blog.model.Artical;
import cn.hairui.blog.repository.ArticalRepository;
import cn.hairui.blog.service.ArticalService;
import cn.hairui.blog.util.CommUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author laoganbu
 * @date: 2019/3/4 16:51
 */
@Service(value = "articalService")
public class ArticalServiceImpl implements ArticalService {
    @Autowired
    private ArticalRepository articalRepository;

    @Override
    public int add(Artical artical) {
        return articalRepository.add(artical);
    }

    @Override
    public int update(Artical artical) {
        return articalRepository.update(artical);
    }

    @Override
    public Artical find(int pk) {
        return articalRepository.find(pk);
    }

    @Override
    public int delete(int pk) {
        return articalRepository.delete(pk);
    }

    @Override
    public List<Artical> queryByNavid(int navid, int count) {
        return articalRepository.queryByNavid(navid, count);
    }

    @Override
    public List<Artical> findByCategoryIdLimited(Integer categoryId, int count) {
        return articalRepository.findByCategoryIdLimited(categoryId, count);
    }

    @Override
    public List<Artical> findHotLimited(int count) {
        return articalRepository.findHotLimited(count);
    }

    @Override
    public List<Artical> findTop() {
        return articalRepository.findTop();
    }

    @Override
    public List<Artical> list() {
        return articalRepository.list();
    }

    @Override
    public List<Map> findtagsLimited(int count) {

        List<String> result = new LinkedList<>();
        List<String> tags = articalRepository.findAllTags(count);
        for (String tag : tags
        ) {
            String[] args = tag.split("/");
            for (String arg : args
            ) {

                if (!result.contains(arg.replaceAll("#", ""))) {
                    result.add(arg.replaceAll("#", ""));
                }
            }
        }
        if (result.size() > count) {
            result = result.subList(0, count);
        }

        List<Map> result2 = new LinkedList<>();
        Iterator iterator = result.iterator();
        while (iterator.hasNext()) {
            Map map = new HashMap();
            String tagName = (String) iterator.next();
            //
            int counts = articalRepository.findCountBytag(tagName);
            map.put("name", tagName);
            map.put("count", counts + "");
            map.put("color", CommUtil.createRandomColor());
            result2.add(map);
        }
        return result2;
    }

    @Override
    public List<Artical> findByCategoryId(Integer categoryId) {
        return articalRepository.findByCategoryId(categoryId);
    }

    @Override
    public List<Map> listArchive() {
        return articalRepository.listArchive();
    }

    @Override
    public List<Artical> findByCreateDateYm(String dateStr) {
        return articalRepository.findByCreateDateYm(dateStr);
    }

    @Override
    public List<Artical> findByKeyword(String keyword) {
        return articalRepository.findByKeyword(keyword);
    }

    @Override
    public List<Map> listMgr() {
        return articalRepository.listMgr();
    }

    @Override
    public int countMgr() {
        return articalRepository.countMgr();
    }

    @Override
    public void updateIntopSts(int id, String flag) {
        articalRepository.updateIntopSts(id, flag);
    }

    @Override
    public void updateContent(Artical artical) {
        articalRepository.updateContent(artical);
    }

    @Override
    public List<String> findtags() {

        List<String> result = new LinkedList<>();
        List<String> tagsList = articalRepository.findtags();
        for (String tag : tagsList
        ) {
            String[] args = tag.split("/");
            for (String arg : args
            ) {

                if (!result.contains(arg.replaceAll("#", ""))) {
                    result.add(arg.replaceAll("#", ""));
                }
            }
        }

        return result;
    }

    @Override
    public int findCountByTagName(String tagName) {
        return articalRepository.findCountBytag(tagName);
    }

    @Override
    public List<Artical> findByTags(String tag) {
        return articalRepository.findByTags(tag);
    }

    @Override
    public Artical findLast(int id) {
        return articalRepository.findLast(id);
    }

    @Override
    public Artical findNext(int id) {
        return articalRepository.findNext(id);
    }

    @Override
    public List<Artical> findByTopicId(Integer topicId) {
        return articalRepository.findByTopicId(topicId);
    }
}
